package main

import (
	"fmt"
	"math/rand"
	"strconv"
	"strings"
	"time"
)

//作业14 做一个抽奖程序, 中奖率万分之一, 看看你多少个10连能中奖

func main() {

	var arr []int = make([]int, 10000)
	rand.Seed(time.Now().UnixNano())
	num := rand.Intn(10000)
	arr[num] = 1
	var r string
	var isOK bool
	for {
		time.Sleep(time.Duration(10) * time.Millisecond)
		rand.Seed(time.Now().UnixNano())
		a := rand.Intn(10)
		b := rand.Intn(10)
		fmt.Printf("请回答如下问题,%d+%d的值是多少(输入exit退出): ", a, b)
		fmt.Scan(&r)
		if strings.Contains(r, "exit") {
			break
		}
		nums, _ := strconv.ParseInt(r, 0, 0)
		var chou = int(nums)
		if a+b != chou {
			continue
		}
		fmt.Printf("恭喜你，回答正确，随机获取%d次抽奖机会\n", chou)
		fmt.Println("奖池大小", len(arr))
		isOK, arr = GoGet(chou, arr)
		if isOK {
			fmt.Print("恭喜中奖")
			break
		}
		fmt.Println("未中奖")
	}
}

func GoGet(num int, arr []int) (bool, []int) {
	var isOK = false
	for i := 0; i < num; i++ {
		time.Sleep(time.Duration(10) * time.Millisecond)
		rand.Seed(time.Now().UnixNano())
		res := rand.Intn(len(arr))
		//fmt.Println(res)
		if arr[res] == 1 {
			isOK = true
			break
		}
		arr = append(arr[:res], arr[res+1:]...)
	}
	return isOK, arr
}
